Skip to content

[mlir][vector] Use llvm::Align when constructing vector load and stores. #152207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 7, 2025

Conversation

amd-eochoalo
Copy link
Contributor

@amd-eochoalo amd-eochoalo commented Aug 5, 2025

This patchset uses llvm::Align when constructing vector.{load,store} operations. The use of llvm::Align allows us to specify the unit of alignment and strongly type alignment as opposed to having just unsigned integers.

@amd-eochoalo amd-eochoalo marked this pull request as ready for review August 5, 2025 21:14
@amd-eochoalo amd-eochoalo requested a review from kuhar as a code owner August 5, 2025 21:14
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-vector

Author: Erick Ochoa Lopez (amd-eochoalo)

Changes

This patchset adds a small struct which represents the alignment in terms of bytes. Adding an explicit struct instead of using integers allows for easier reading and avoids confusion since the alignment can be given in different units. See for example the following RFC.


Full diff: https://github.com/llvm/llvm-project/pull/152207.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/Vector/IR/VectorOps.h (+9)
  • (modified) mlir/include/mlir/Dialect/Vector/IR/VectorOps.td (+6-6)
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.h b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.h
index 364c1728715e8..b5ae0123d8c53 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.h
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.h
@@ -77,6 +77,15 @@ struct VectorDim {
   int64_t dim;
   bool isScalable;
 };
+
+struct AlignmentBytes {
+  uint64_t alignment = 0;
+  AlignmentBytes() = default;
+  explicit AlignmentBytes(uint64_t alignment) : alignment(alignment) {};
+  explicit operator bool() const { return alignment; }
+  uint64_t getAlignment() const { return alignment; }
+};
+
 BroadcastableToResult
 isBroadcastableTo(Type srcType, VectorType dstVectorType,
                   std::pair<VectorDim, VectorDim> *mismatchingDims = nullptr);
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index 3885439e11f89..3ebb5721d7181 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -1729,18 +1729,18 @@ def Vector_LoadOp : Vector_Op<"load", [
                    "Value":$base,
                    "ValueRange":$indices,
                    CArg<"bool", "false">:$nontemporal,
-                   CArg<"uint64_t", "0">:$alignment), [{
+                   CArg<"AlignmentBytes", "AlignmentBytes()">: $alignment), [{
       return build($_builder, $_state, resultType, base, indices, nontemporal,
-                   alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
+                   alignment ? $_builder.getI64IntegerAttr(alignment.getAlignment()) :
                                     nullptr);
     }]>,
     OpBuilder<(ins "TypeRange":$resultTypes,
                    "Value":$base,
                    "ValueRange":$indices,
                    CArg<"bool", "false">:$nontemporal,
-                   CArg<"uint64_t", "0">:$alignment), [{
+                   CArg<"AlignmentBytes", "AlignmentBytes()">: $alignment), [{
       return build($_builder, $_state, resultTypes, base, indices, nontemporal,
-                   alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
+                   alignment ? $_builder.getI64IntegerAttr(alignment.getAlignment()) :
                                     nullptr);
     }]>
   ];
@@ -1847,9 +1847,9 @@ def Vector_StoreOp : Vector_Op<"store", [
                    "Value":$base,
                    "ValueRange":$indices,
                    CArg<"bool", "false">:$nontemporal,
-                   CArg<"uint64_t", "0">:$alignment), [{
+                   CArg<"AlignmentBytes", "AlignmentBytes()">:$alignment), [{
       return build($_builder, $_state, valueToStore, base, indices, nontemporal,
-                   alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
+                   alignment ? $_builder.getI64IntegerAttr(alignment.getAlignment()) :
                                     nullptr);
     }]>
   ];

Copy link
Contributor

@fabianmcg fabianmcg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for starting the conversation on this!

Since we haven't had the discussion on bit alignments on the RFC, and LLVM already has llvm::Align and uses it for the load instruction, I think it's better to keep status quo or use llvm::Align instead of introducing a new struct.

@amd-eochoalo
Copy link
Contributor Author

amd-eochoalo commented Aug 6, 2025

@fabianmcg thanks! I didn't know about this struct. I've reverted the changes I had and used llvm::Align instead.

@amd-eochoalo amd-eochoalo changed the title [mlir][vector] Add AlignmentBytes struct [mlir][vector] Use llvm::Align when constructing vector load and stores. Aug 6, 2025
@kuhar kuhar requested a review from fabianmcg August 6, 2025 16:34
Copy link
Contributor

@fabianmcg fabianmcg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! LGTM

@fabianmcg
Copy link
Contributor

fabianmcg commented Aug 6, 2025

Please update the description of the commit before merging.

@kuhar
Copy link
Member

kuhar commented Aug 6, 2025

Please update the description of the commit before merging.

I think you ultimately mean the PR description that gets used for the commit message after squashing?

@fabianmcg
Copy link
Contributor

I think you ultimately mean the PR description that gets used for the commit message after squashing?

Yes.

Revert "add comment"
This reverts commit 502bd1e.

Revert "Make bool operator explicit"
This reverts commit 1a68ecf.

Revert "Style"
This reverts commit 3ce76bc.

Revert "Add AlignmentBytes class"
This reverts commit 8798040.
@kuhar kuhar merged commit 44fbeb3 into llvm:main Aug 7, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants